const express=require('express');
const router= express.Router();
var data = fs.readFileSync('./words.json');
var words = JSON.parse(data);
const User =require('../models/users');

// router.use(function(req, res, next) {
//   res.header("Access-Control-Allow-Origin", "*"); 
//   res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
//   next();
// });
router.get('/gwork',function(req,res){
    res.send('这是子域名gwork')
});

router.post('/login/',function(req,res){
    // console.log(req.params.userdata)
    // res.send(req.ip)
    // var user=new User(req.params.userdata.value)
    console.log(req.body)
    var user=new User(req.body)
    user.save();
    res.send('已加入数据库'+req.body.name)
})

router.get('/login/:userdata',function(req,res){
    console.log(req.params.userdata)
    res.send(req.ip)
    var user=new User(req.params.userdata.value)
})

router.get('/add/:word/:score', addWord);
router.get('/add/:accessname', addID);



router.get('/latestquestions', loadLatestQ)
function loadLatestQ(req,res){
  // User.find({$or: [{'questions.编辑好的主观题': { $ne: null }},{'questions.编辑好的问答题':{ $ne :null}}]})
  User.find( {$or: [
    {'questions.编辑好的问答题': { $exists: true}},
   { 'questions.编辑好的主观题': { $exists: true }}
 ]}).sort({_id:-1}). exec(function(err, userfound){
  if(err){
      console.log(err);
  }
  else{
      // if (userfound.questions.编辑好的选择题){}
      console.log(userfound.slice(0,9))
      res.send(userfound)
  }
}
)

}


function addWord(request, response) {

  var word = request.params.word;
  var score = request.params.score;
  var reply;
  if (!score) {
    var reply = {
      msg: '请输入分数.'
    };
    response.send(reply);
  } else {
     
     
     
     var word_timekey = Date.now()
     word_time={}
     word_time[word_timekey]= score
     var data ={word:`word:{"${word}":${JSON.stringify(word_time)}}`};
     console.log(data)
     var user=new User(data)
     user.save()
     words[word]=score
    fs.writeFile('./words.json',JSON.stringify(words),finished)
     
  }
  
  
function finished() {
    console.log('已添加.');
    
    response.send(user.word);
   
  }
  


  }


//   function addID(request, response) {
//     var data = request.params;
//     var accessid = data.accessid;
//     var time = data.time;
//     var reply;
//     if (!time) {
//       var reply = {
//         msg: '请输入时间.'
//       };
//       response.send(reply);
//     } else {
//       IDs[accessid] = time;
//       var data = JSON.stringify(IDs, null, 2);
//       fs.writeFile('acoounts.json', ','+data, finishedID);
//     }
      
//   function finishedID(err) {
//     console.log('已添加id.');
//     reply = {
//       id: accessid,
//       time: IDs[accessid],
//       status: 'success'
//     };
//     response.send(reply);
//   }
//     }
function addID(request, response) {

    var accessname = request.params.accessname;
    var time = Date.now()
      UserIdentity={}
      UserIdentity[accessname] = time;
      var data ={}
      data["name"] = JSON.stringify(UserIdentity);
      user= new User(data)
      user.save()
      finishedID;
    }
      
  function finishedID(err) {
    console.log('已添加id.');
    reply = {
      name: accessname,
      time: UserIdentity[accessname],
      status: 'success'
    };
    response.send(reply);
  }
    
   

module.exports = router